package com.zhiyou100.crm.dao.impl;

import com.zhiyou100.crm.dao.CustomerStateDao;
import com.zhiyou100.crm.util.Customer;
import com.zhiyou100.crm.util.CustomerState;
import com.zhiyou100.crm.util.DBUtil1;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CustomerStateDaoImpl implements CustomerStateDao {
    DBUtil1 dbUtil1=new DBUtil1();
    ResultSet rs=null;

    @Override
    public List<CustomerState> query() {
        List<CustomerState> customerStates=new ArrayList<>();
        String sql="select * from customer_state";
        rs=dbUtil1.select(sql,null);
        try {
            while (rs.next()) {
                int customer_state_id = rs.getInt("customer_state_id");
                int status = rs.getInt("status");
                int creater = rs.getInt("creater");
                int updater = rs.getInt("updater");
                String customer_state_name = rs.getString("customer_state_name");
                String customer_state_desc = rs.getString("customer_state_desc");
                String remark = rs.getString("remark");
                Timestamp create_time = rs.getTimestamp("create_time");
                Timestamp update_time = rs.getTimestamp("update_time");
                CustomerState customerState=new CustomerState(customer_state_id,customer_state_name,customer_state_desc,status,remark,create_time,creater,update_time,updater);
                customerStates.add(customerState);
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }finally {
            dbUtil1.close();
        }
        return customerStates;
    }

    @Override
    public int delete(int id) {
        String sql="delete from customer_state where customer_state_id=?";
        Object[] obj={id};
        return  dbUtil1.update(sql,obj);
    }

    @Override
    public int update(CustomerState customerState) {
        String sql="update customer_state set customer_state_name=?,customer_state_desc=?,update_time=?,updater=? where customer_state_id=?";
        Object[] obj={customerState.getCustomer_state_name(),customerState.getCustomer_state_desc(),customerState.getUpdate_time(),customerState.getUpdater(),customerState.getCustomer_state_id()};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public int insert(CustomerState customerState) {
        String sql="insert into customer_state(customer_state_name,customer_state_desc,create_time,creater) values(?,?,?,?)";
        Object[] obj={customerState.getCustomer_state_name(),customerState.getCustomer_state_desc(),customerState.getCreate_time(),customerState.getCreater()};
        return dbUtil1.update(sql,obj);
    }
}
